package cz.muni.fi.xvano1.trinity.beans;

import cz.muni.fi.xvano1.trinity.core.Core;
import cz.muni.fi.xvano1.trinity.supplementary.ActionManagerBeanContext;
import cz.muni.fi.xvano1.trinity.supplementary.DataSupplier;
import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sourceforge.stripes.action.*;

/**
 * This class provides options for administrator. Due to the framework nature,
 * getter and setter methods for parameters of request have been implemented
 *
 * @author Jan Vano
 */
public class AdminActionBean implements ActionBean {

    private ActionManagerBeanContext context;
    private String user = null;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    /**
     * Interface method from ActionBean.
     */
    @Override
    public void setContext(ActionBeanContext context) {
        this.context = (ActionManagerBeanContext) context;
    }

    /**
     * Interface method from ActionBean, using a co-variant return type.
     */
    @Override
    public ActionManagerBeanContext getContext() {
        return this.context;
    }

    /**
     * Redirect user to homepage.
     *
     * @return Resolution objct to framework.
     */
    @DefaultHandler
    public Resolution home() {
        return new RedirectResolution("/Home");
    }

    /**
     * Redirect administrator to page with admin options.
     *
     * @return Resolution objct to framework.
     */
    public Resolution displayOptions() {
        return new RedirectResolution("/Admin");
    }

    /**
     * Redirect administrator to page with input for impersonation.
     *
     * @return Resolution objct to framework.
     */
    public Resolution impersonate() {
        if (user == null) {
            getContext().getRequest().setAttribute("changeUser", true);
        } else {
            getContext().getRequest().getSession().setAttribute("user", user);
            getContext().getRequest().setAttribute("changeUser", false);
        }
        return new ForwardResolution("/Admin");
    }

    /**
     * Invalid impersonating and forward to admin page.
     *
     * @return Resolution objct to framework.
     */
    public Resolution resetUser() {
        getContext().getRequest().setAttribute("changeUser", false);
        getContext().getRequest().getSession().setAttribute("user",
                getContext().getRequest().getAttribute("loggedUser"));
        return new ForwardResolution("/Admin");
    }

    /**
     * Print system configuration.
     *
     * @return Resolution objct to framework.
     */
    public Resolution printSysConf() {
        getContext().getRequest().setAttribute("showOptions", true);
        return new ForwardResolution("/Admin");
    }

    /**
     * Print core system configuration.
     *
     * @return Resolution objct to framework.
     */
    public Resolution core() {
        String response = "";
        String configFile = Core.configDirectory + "config.dat";
        BufferedReader r;
        try {
            r = new BufferedReader(new FileReader(configFile));
            String temp;
            while ((temp = r.readLine()) != null) {
                response += temp + "<br/>";
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CalendarViewActionBean.class.getName()).log(Level.SEVERE, null, ex);
            response = "Exception occured while opening config file.";
        } catch (IOException ex) {
            Logger.getLogger(CalendarViewActionBean.class.getName()).log(Level.SEVERE, null, ex);
            response = "Exception occured while reading config file.";
        }
        getContext().getRequest().setAttribute("showOptions", true);
        getContext().getRequest().setAttribute("printConf", true);
        getContext().getRequest().setAttribute("configData", response);
        return new ForwardResolution("/Admin");
    }

    /**
     * Print list of users active in the system.
     *
     * @return Resolution objct to framework.
     */
    public Resolution userList() {
        List<String> users = DataSupplier.getUsers();
        getContext().getRequest().setAttribute("showOptions", true);
        getContext().getRequest().setAttribute("printUsers", true);
        getContext().getRequest().setAttribute("users", users);
        return new ForwardResolution("/Admin");
    }
}
